<template>
  <el-dialog title="分配角色" :visible.sync="diaglogShow">
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>

    <div style="text-align: center;margin-top:20px;">
      <el-button type="primary" @click="save">确认</el-button>
      <el-button @click="diaglogShow = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { roleListAPI } from '@/api/role'
import { profile, asignRoleAPI } from '@/api/employees'
export default {
  data() {
    return {
      diaglogShow: false,
      checkList: [],
      roleList: [],
      id: ''
    }
  },
  methods: {
    async show(id) {
      // 把用户id存起来
      this.id = id
      // 1. 获取所有角色
      const res = await roleListAPI({
        page: 1,
        pagesize: 99999999
      })
      this.roleList = res.data.rows

      // 2. 设置当前用户拥有的角色选中
      const res2 = await profile(id)
      this.checkList = res2.data.roleIds

      // 3. 显示出对话框
      this.diaglogShow = true
    },

    async save() {
      await asignRoleAPI({
        id: this.id,
        roleIds: this.checkList
      })
      this.$message.success('分配角色成功')
      this.diaglogShow = false
    }
  }
}
</script>

<style>

</style>
